package com.tiniplanet.webgate.web.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tiniplanet.webgate.entity.News;
import com.tiniplanet.webgate.service.NewsService;

@Controller
public class GeneralController extends PageController {
	private static final Logger logger = Logger.getLogger(GeneralController.class);

	@Autowired
	NewsService newsService;

	public GeneralController() {
		// setup tab menu
		addItemToTabMenu("/news", "News", "dashboard", true);
		addItemToTabMenu("/report", "Report", "reports", false);

		// setup side menu
		addItemToSideMenu("/home", "General", "home", true);
		addItemToSideMenu("/admin/", "Admin", "form", false);
	}

	@RequestMapping(method = RequestMethod.GET, value = "/home")
	public String homePage(ModelMap model) {
		logger.debug("Request home page");
		return "redirect:news";
	}

	@RequestMapping(method = RequestMethod.GET, value = "/news")
	public String setupNewsPage(ModelMap model) {
		logger.debug("Request news page");
		resetTabMenu();
		tabMenu.get(TAB_MENU_NEWS_INDEX).setActive(true);

		setModel(model, null);

		List<News> news = newsService.getAll();
		model.addAttribute("news", news);
		
		setMenu(model);		

		return "news";
	}

	@RequestMapping(method = RequestMethod.GET, value = "/report")
	public String setupReportPage(ModelMap model) {
		logger.debug("Request report page");
		resetTabMenu();
		tabMenu.get(TAB_MENU_REPORT_INDEX).setActive(true);
		
		setMenu(model);
		
		setModel(model, null);
		
		return "report";
	}

}
